<?php namespace App\Models\Power;

use App\Models\Base\PowerBase;
use App\Models\Power\Pile;

class Price extends PowerBase {

    protected $table = 'cdz_electric_price';

    protected $fillable = [];

    protected $dates = [];

    public static $rules = [
        // Validation rules
    ];

    /**
     * 根据枪口号查询电桩充电费用
     * params1 String $no
     * params2 Int $pile_id
     */
    public function getPilePrice($pile_no, $pile_id) {

        $result = ['price'=>[], 'max_electric'=>0, 'min_electric'=>0, 'max_service'=>0, 'min_service'=>0];
        $query = self::query();
        $query -> where('pile_id', $pile_id);
        $price = $query -> get() -> toArray();
        
        //初始化最大最小值
        $max_electric = 0;
        $max_service = 0;

        //获取当前是几时
        $current = intval(date("H",time()));
        if($price) {
            //初始化最大最小值
            $max_electric = $price[0]['electricity_fees'];
            $max_service = $price[0]['service_charge'];
            $min_electric = $price[0]['electricity_fees'];
            $min_service = $price[0]['service_charge'];
            foreach($price as $key => $value) {

                $temp['during'] = '';
                $temp['electric'] = 0;
                $temp['service'] = 0;
                $temp['price'] = 0;
                $temp['during'] = str_pad($value['begin_h'], 2, '0', STR_PAD_LEFT) . ':' . str_pad($value['begin_m'], 2, '0', STR_PAD_LEFT) . '-' .str_pad($value['end_h'], 2, '0', STR_PAD_LEFT) . ':' . str_pad($value['end_m'], 2, '0', STR_PAD_LEFT);
                $temp['electric'] = $value['electricity_fees'];

                if(round($value['electricity_fees'],2) > round($max_electric,2)) {
                    $max_electric = $value['electricity_fees'];
                }
                if(round($value['service_charge'],2) > round($max_electric,2)) {
                    $max_service = $value['service_charge'];
                }

                if(round($value['electricity_fees'],2) < round($min_electric,2)) {
                    $min_electric = $value['electricity_fees'];
                }
                if(round($value['service_charge'],2) < round($min_electric,2)) {
                    $min_service = $value['service_charge'];
                }

                $temp['service'] = $value['service_charge'];
                $temp['price'] = $temp['electric'] + $temp['service'];
                if($current > intval(str_pad($value['begin_m'], 2, '0', STR_PAD_LEFT)) && $current < intval(str_pad($value['end_h'], 2, '0', STR_PAD_LEFT))) {
                    $temp['is_current'] = 1;
                }

                $result['price'][] = $temp;

            }

            $result['max_electric'] = $max_electric;
            $result['min_electric'] = $min_electric;
            $result['max_service'] = $max_service;
            $result['min_service'] = $max_service;

        }

        return $result;
        
    }

    /**
     * 和Pile模型的多对一关联
     */
    public function pile() {
        return $this->belongsTo('App\Models\Pile', 'pile_id');
    }



}
